package com.jxust.vhr.service.impl;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jxust.debate.pojo.*;
import com.jxust.debate.util.CommonResult;
import com.jxust.debate.util.ResultEnum;
import com.jxust.vhr.dao.*;
import com.jxust.vhr.service.CompanyAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class CompanyAdminServiceImpl implements CompanyAdminService {

    @Autowired
    private CompanyAdministratorDao companyAdministratorDao;

    @Autowired
    private CacodeDao cacodeDao;

    @Autowired
    private DepartmentDao departmentDao;

    @Autowired
    private CompanyDao companyDao;

    @Autowired
    private DmcodeDao dmcodeDao;

    /**
     * 验证账号唯一性
     * @param caaccount 账号
     * @return 返回成功信息
     */
    @Override
    public CommonResult VerifyAccount(String caaccount) {
        CommonResult commonResult=new CommonResult();
        QueryWrapper<CompanyAdministrator> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("caaccount",caaccount);
        int count=companyAdministratorDao.selectCount(queryWrapper);
        if(count==0){
            commonResult.setResultEnum(ResultEnum.SUCCESS);
        }else {
            commonResult.setCode(ResultEnum.FAILURE.getCode());
            commonResult.setMessage("账号已存在");
        }
        return commonResult;
    }

    /**
     * 注册
     * @param companyAdministrator 注册信息
     * @param cacode 认证码
     * @return 返回成功信息
     */
    @Override
    public CommonResult Register(CompanyAdministrator companyAdministrator, Cacode cacode) {
        CommonResult commonResult=new CommonResult();
        companyAdministrator.setCaid(RandomUtil.randomString(32));
        cacode.setCaid(companyAdministrator.getCaid());
        companyAdministratorDao.insert(companyAdministrator);
        cacodeDao.updateById(cacode);
        commonResult.setResultEnum(ResultEnum.SUCCESS);
        return commonResult;
    }

    /**
     * 登录
     * @param companyAdministrator 账号与密码
     * @return 返回登录信息（密码置空)
     */
    @Override
    public CommonResult<CompanyAdministrator> Login(CompanyAdministrator companyAdministrator) {
        CommonResult<CompanyAdministrator> commonResult=new CommonResult<>();
        QueryWrapper<CompanyAdministrator> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("caaccount",companyAdministrator.getCaaccount());
        queryWrapper.eq("capassword",companyAdministrator.getCapassword());
        companyAdministrator=companyAdministratorDao.selectOne(queryWrapper);
        if(companyAdministrator!=null&& !StrUtil.hasEmpty(companyAdministrator.getCaid())){
            commonResult.setResultEnum(ResultEnum.USER_LOGIN_SUCCESS);
            companyAdministrator.setCapassword("");
            commonResult.setData(companyAdministrator);
        }else {
            commonResult.setResultEnum(ResultEnum.USER_LOGIN_FAILED);
        }
        return commonResult;
    }

    /**
     * 查询此管理员所属公司信息
     * @param caid 管理员ID
     * @return 返回公司信息
     */
    @Override
    public CommonResult<Company> Affiliation(String caid) {
        CommonResult<Company> commonResult=new CommonResult();
        Company company;
        company =companyAdministratorDao.selectCompanyByCompanyAdminID(caid);
        if(company!=null&& !StrUtil.hasEmpty(company.getCid())) {
            commonResult.setResultEnum(ResultEnum.SUCCESS);
            commonResult.setData(company);
        }else {
            commonResult.setCode(ResultEnum.FAILURE.getCode());
            commonResult.setMessage("无此ID");
        }
        return commonResult;
    }

    /**
     * 创建公司部门
     * @param department 公司信息
     * @return 返回创建信息
     */
    @Override
    public CommonResult CreatDepartment(Department department) {
        CommonResult commonResult=new CommonResult();
        try {
            departmentDao.insert(department);
        }catch (Exception e){
            commonResult.setCode(ResultEnum.FAILURE.getCode());
            commonResult.setMessage(e.getMessage());
        }
        commonResult.setResultEnum(ResultEnum.SUCCESS);
        return commonResult;
    }

    /**
     * 为部门管理员或HR创建认证码
     * @param cid 传入公司ID
     * @return 返回创建的认证码（返回的认证码首两字母为DM）
     */
    @Override
    public CommonResult<Dmcode> CodeCreate(String cid) {
        CommonResult<Dmcode> commonResult =new CommonResult<>();
        Company company=companyDao.selectById(cid);
        //查看公司ID是否正确
        if(company!=null && !StrUtil.hasEmpty(company.getCid())){
            Dmcode dmcode=new Dmcode();
            dmcode.setCid(company.getCid());
            dmcode.setCode("DM"+RandomUtil.randomString(30));
            dmcodeDao.insert(dmcode);
            commonResult.setResultEnum(ResultEnum.SUCCESS);
            commonResult.setData(dmcode);
        }else {
            commonResult.setResultEnum(ResultEnum.FAILURE);
        }
        return commonResult;
    }
}
